<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <button id="addNewObserver">订阅</button>
    <input id="mainCheckbox" type="checkbox" />发布
    <div id="observersContainer"></div>
    <script src="./PubSub.js"></script>
    <script>
        const pubSub = new PubSub();
        const controlCheckbox = document.getElementById("mainCheckbox"),
            addBtn = document.getElementById("addNewObserver"),
            container = document.getElementById("observersContainer");

        //发布
        controlCheckbox.addEventListener("click", function () {
            pubSub.publish("pu", controlCheckbox.checked);

        })
        addBtn.addEventListener("click", function () {
            var check = document.createElement("input");
            check.type = "checkbox";
            //订阅
            const update = function (value) {
                this.checked = value;
            }.bind(check);
            pubSub.subscribe("pu", update)
            container.appendChild(check);
        })
    </script>
</body>

</html>
